<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: fight
  Date: 2019/9/20
  Time: 9:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
    <meta name="viewport" content="width=device-width"/>
    <link rel="stylesheet" href="css/index.css" type="text/css" />
</head>
<body>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<div class="main">
    <div class="login-box">
        <form action="${ctx}/login">
            <div class="form-item">
                <label for="username" class="form-item-label">用户名：</label>
                <div class="form-input">
                    <input type="text" name="username" id="username" placeholder="请输入用户名" value="admin" />
                </div>
            </div>
            <div class="form-item">
                <label for="password" class="form-item-label">密码：</label>
                <div class="form-input">
                    <input type="text" name="username" id="password" value="123" placeholder="请输入用户名" autocomplete="off" />
                </div>
            </div>
            <div class="form-btn">
                没有账号？<a href="${ctx}/register">注册</a>
                <button type="button" class="btn btn-success" id="submit">登录</button>
            </div>
        </form>
    </div>

</div>
<script src="${ctx}/js/jquery.min.js"></script>
<script>
    $('#submit').on('click', function () {
        var username = $('#username').val();
        var password = $('#password').val();
        if(!$.trim(username)){
            alert('用户名不能为空');
            return;
        }
        if(!$.trim(password)){
            alert('密码不能为空');
            return;
        }
        $.ajax({
            url:'${ctx}/login',
            type:'post',
            data:{username, password},
            success:(msg)=>{
                if(msg=='ok'){
                    window.location.href = '${ctx}/index';
                }else{
                    alert(msg);
                }
            }
        });
    })
</script>
</body>
</html>
